package com.topvision.controller.zwan;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

import com.topvision.controller.AbstractController;
import com.topvision.model.zwan.Companydevice;
import com.topvision.model.zwan.DeviceVendorMap;
import com.topvision.service.zwan.CompanydeviceService;
import com.topvision.service.zwan.DeviceVendorMapService;
import com.topvision.service.zwan.DeviceVendorService;
import com.topvision.util.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.topvision.model.zwan.Sysorderdeviceinfo;
import com.topvision.service.zwan.SysorderdeviceinfoService;
import com.topvision.util.PageUtils;
import com.topvision.util.Query;
import com.topvision.util.R;

import javax.servlet.http.HttpServletRequest;

/**
 * @author shengwm
 * @email 179653076@qq.com
 * @date 2017-08-08 09:42:41
 */
@RestController
@RequestMapping("zwan/sysorderdeviceinfo")
public class SysorderdeviceinfoController extends AbstractController {
    @Autowired
    private SysorderdeviceinfoService sysorderdeviceinfoService;
    @Autowired
    private CompanydeviceService companydeviceService;
    @Autowired
    private DeviceVendorMapService deviceVendorMapService;
    @Autowired
    private DeviceVendorService deviceVendorService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("zwan/sysorderdeviceinfo:list")
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<Sysorderdeviceinfo> sysorderdeviceinfoList = sysorderdeviceinfoService.queryList(query);
        int total = sysorderdeviceinfoService.queryTotal(query);
        List<String> strings = new ArrayList<>();
        //java 8 lambda写法 遍历设备列表
        if (sysorderdeviceinfoList != null && sysorderdeviceinfoList.size() > 0) {
            sysorderdeviceinfoList.forEach(sysorderdeviceinfo -> strings.add(sysorderdeviceinfo.getDeviceid()));
            List<DeviceVendorMap> deviceVendorMaps = deviceVendorMapService.findByDeviceids(strings);
            if (deviceVendorMaps != null && deviceVendorMaps.size() > 0) {
                sysorderdeviceinfoList.forEach(sysorderdeviceinfoex -> deviceVendorMaps.forEach(deviceVendorMap -> {
                    if (sysorderdeviceinfoex.getDeviceid() != null && deviceVendorMap.getDeviceid() != null) {
                        if (sysorderdeviceinfoex.getDeviceid().equals(deviceVendorMap.getDeviceid())) {
                            sysorderdeviceinfoex.setVendorcode(deviceVendorMap.getVendorcode());
                            sysorderdeviceinfoex.setVendorname(deviceVendorMap.getVendorname());
                        }
                    }
                }));
            }
        }

        PageUtils pageUtil = new PageUtils(sysorderdeviceinfoList, total, query.getLimit(), query.getPage());

        return R.success().put("page", pageUtil);
    }

    /**
     * 列表
     */
    @RequestMapping("/findByParam")
    @RequiresPermissions("zwan/sysorderdeviceinfo:list")
    public R findByParam(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<Sysorderdeviceinfo> sysorderdeviceinfoList = sysorderdeviceinfoService.queryByParam(query);
        int total = sysorderdeviceinfoService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(sysorderdeviceinfoList, total, query.getLimit(), query.getPage());

        return R.success().put("page", pageUtil);
    }

    /**
     * 列表
     */
    @RequestMapping("/findByDeviceId/{deviceId}")
    @RequiresPermissions("zwan/sysorderdeviceinfo:list")
    public R findByDeviceId(@PathVariable String deviceId) {
        Map<String, Object> params = new HashMap<>();
        params.put("deviceid", deviceId);
        //查询列表数据
        List<Sysorderdeviceinfo> sysorderdeviceinfoList = sysorderdeviceinfoService.findByParams(params);
        if (sysorderdeviceinfoList != null && sysorderdeviceinfoList.size() > 0) {
            return R.success(sysorderdeviceinfoList.get(0));
        }
        return R.success(null);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{deviceseqno}")
    @RequiresPermissions("zwan/sysorderdeviceinfo:info")
    public R info(@PathVariable("deviceseqno") BigDecimal deviceseqno) {
        Sysorderdeviceinfo sysorderdeviceinfo = sysorderdeviceinfoService.queryObject(deviceseqno);
        if (sysorderdeviceinfo != null) {
            List<String> arr = new ArrayList<>();
            arr.add(sysorderdeviceinfo.getDeviceid());
            List<DeviceVendorMap> maps = deviceVendorMapService.findByDeviceids(arr);
            if (maps != null && maps.size() > 0) {
                sysorderdeviceinfo.setVendorcode(maps.get(0).getVendorcode());
            }
        }
        return R.success().put("sysorderdeviceinfo", sysorderdeviceinfo);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("zwan/sysorderdeviceinfo:save")
    public R save(@RequestBody Sysorderdeviceinfo sysorderdeviceinfo) {
        //sysorderdeviceinfoService.save(sysorderdeviceinfo);
        String num = "1";
        String devicetype = "301";
        String vendorid = "3";
        System.out.println("插入设备：" + devicetype + " " + vendorid + " " + num);
        Map<String, Object> map = new HashMap<String, Object>();
       /* map.put("devicevendorid", sysorderdeviceinfo.getDevicevendorid());
        map.put("devicetype",sysorderdeviceinfo.getDevicetype());*/
        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMddHHmmss");
        String dateString = formatter.format(currentTime);
        map.put("devicevendorid", vendorid);
        map.put("devicetype", devicetype);
        map.put("devicenum", "1");
        sysorderdeviceinfo.setBatchid(dateString);
        sysorderdeviceinfo.setDevicetype(BigDecimal.valueOf(301));
        sysorderdeviceinfo.setDevicevendorid(BigDecimal.valueOf(3));
       /* try {
            sysorderdeviceinfoService.batchInsert(map);
            DeviceVendorMap deviceVendorMap = new DeviceVendorMap();
          //  deviceVendorMap.setDeviceid();
        } catch (Exception e) {
            e.printStackTrace();
        }
        if(map.get("result").toString().equals("1")) {
            System.out.println(num + "个设备导入成功");
        }*/

        sysorderdeviceinfoService.save(sysorderdeviceinfo);

        if (sysorderdeviceinfo.getVendorcode() != null) {
            DeviceVendorMap deviceVendorMap = new DeviceVendorMap();
            deviceVendorMap.setVendorcode(sysorderdeviceinfo.getVendorcode());
            deviceVendorMap.setDeviceid(sysorderdeviceinfo.getDeviceid());
            Map<String, Object> mapdev = new HashMap<>();
            mapdev.put("deviceid", sysorderdeviceinfo.getDeviceid());
            List<DeviceVendorMap> deviceVendorMaps = deviceVendorMapService.queryByParam(mapdev);
            if (deviceVendorMaps != null && deviceVendorMaps.size() > 0) {
                deviceVendorMapService.update(deviceVendorMaps.get(0));
            } else {
                deviceVendorMapService.save(deviceVendorMap);
            }

        }

        return R.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("zwan/sysorderdeviceinfo:update")
    public R update(@RequestBody Sysorderdeviceinfo sysorderdeviceinfo) {
        sysorderdeviceinfoService.update(sysorderdeviceinfo);
        if (sysorderdeviceinfo.getVendorcode() != null) {
            DeviceVendorMap deviceVendorMap = new DeviceVendorMap();
            deviceVendorMap.setDeviceid(sysorderdeviceinfo.getDeviceid());
            deviceVendorMap.setVendorcode(sysorderdeviceinfo.getVendorcode());
            deviceVendorMapService.update(deviceVendorMap);
        }
        return R.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/updatedeviceid")
    @RequiresPermissions("zwan/sysorderdeviceinfo:update")
    public R updatedeviceid(@RequestParam Map<String, Object> params, HttpServletRequest request) {
        Sysorderdeviceinfo sysorderdeviceinfo = new Sysorderdeviceinfo();
        BigDecimal deviceseqno = BigDecimal.valueOf(Long.valueOf(request.getParameter("id")));
        String deviceid = request.getParameter("deviceid");
        if (StringUtils.areNotEmpty(deviceid)) {
            sysorderdeviceinfo.setDeviceid(deviceid);
            sysorderdeviceinfoService.update(sysorderdeviceinfo);
            return R.success();
        } else {
            return R.error("请填入设备编号！！！！");
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("zwan/sysorderdeviceinfo:delete")
    public R delete(@RequestBody BigDecimal[] deviceseqnos) {
        if (deviceseqnos != null && deviceseqnos.length > 0) {
            for (BigDecimal deviceNo : deviceseqnos) {
                Sysorderdeviceinfo sysorderdeviceinfo = sysorderdeviceinfoService.queryObject(deviceNo);
                if (sysorderdeviceinfo != null) {
                    Companydevice companydevice = companydeviceService.queryObject(sysorderdeviceinfo.getDeviceid());
                    if (companydevice != null) {
                        return R.error("该设备已分配给某企业，请先解绑设备！公司ID为：" + companydevice.getCompanyid().toString());
                    } else {
                        sysorderdeviceinfoService.delete(deviceNo);
                        return R.success("删除成功！");
                    }
                }
            }
        }
        return R.error("请选择！");
    }

    /**
     * 解除绑定
     */
    @RequestMapping("/queryBindCompany")
    public R queryBingCompany(@RequestParam Map<String, Object> params) {
        // String deviceid = params.get("deviceid").toString();
        Map<String, Object> map = companydeviceService.queryWithCompany(params);
        return R.success(map).put("bindCompany", map);
    }

}
